package DesignPatern.A07_原型模式.MSB.v4;

/**
 * String需要进一步深克隆吗？
 */
public class Test
{
    public static void main(String[] args) throws Exception
    {
        Person p1 = new Person();
        Person p2 = (Person) p1.clone();
        System.out.println("p1.loc == p2.loc? " + (p1.loc == p2.loc));
        
        p1.loc.street.reverse();//改变p1的loc
        System.out.println(p2.loc.street);//p2的loc也改变了
    }
}

class Person implements Cloneable
{
    int age = 8;
    int score = 100;
    
    Location loc = new Location(new StringBuilder("bj"), 22);
    
    @Override
    public Object clone() throws CloneNotSupportedException
    {
        Person p = (Person) super.clone();
        p.loc = (Location) loc.clone();
        return p;
    }
}

class Location implements Cloneable
{
    StringBuilder street;
    int roomNo;
    
    @Override
    public String toString()
    {
        return "Location{" +
                "street='" + street + '\'' +
                ", roomNo=" + roomNo +
                '}';
    }
    
    public Location(StringBuilder street, int roomNo)
    {
        this.street = street;
        this.roomNo = roomNo;
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }
}
